package family;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Add1 extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;
    JLabel jlpno = new JLabel("工      号：");
    JLabel jlname = new JLabel("姓      名：");
    JLabel jlsex = new JLabel("性      别：");
    JLabel jlage = new JLabel("年       龄：");
    JLabel jlrelation = new JLabel("与本人关系：");
    JLabel jllocation = new JLabel("工 作 地 点：");
    JLabel jlphone = new JLabel( "联 系 电 话：");
    JLabel jlwork = new JLabel("职      务：");

    JTextField jtpno = new JTextField("",20);
    JTextField jtname = new JTextField("",20);
    JTextField jtsex = new JTextField("",20);
    JTextField jtage = new JTextField("",20);
    JTextField jtrelation = new JTextField("",20);
    JTextField jtlocation = new JTextField("",20);
    JTextField jtphone = new JTextField("",20);
    JTextField jtwork = new JTextField("",20);

    JButton buttonadd = new JButton("添加");
    JButton buttonreturn = new JButton("返回");

    public Add1() {
        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpsex = new JPanel();
        JPanel jpage = new JPanel();
        JPanel jprelation = new JPanel();
        JPanel jplocation = new JPanel();
        JPanel jpphone = new JPanel();
        JPanel jpwork = new JPanel();

        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jtname);

        jpsex.add(jlsex);
        jpsex.add(jtsex);

        jpage.add(jlage);
        jpage.add(jtage);

        jprelation.add(jlrelation);
        jprelation.add(jtrelation);

        jplocation.add(jllocation);
        jplocation.add(jtlocation);

        jpphone.add(jlphone);
        jpphone.add(jtphone);

        jpwork.add(jlwork);
        jpwork.add(jtwork);

        jpforbutton.add(buttonadd);
        jpforbutton.add(buttonreturn);

        buttonadd.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {

                boolean flag = false;
                flag = !jtpno.getText().equals("");

                System.out.println(flag);
                if (!flag) {

                    if (e.getSource() == buttonadd) {
                        javax.swing.JOptionPane.showMessageDialog(null, "添加格式有误！！！重新添加！添加操作结束!");
                    }

                } else {

                    Connection conn = null;
                    PreparedStatement ps = null;

                    String xpno = jtpno.getText();
                    String xname = jtname.getText();
                    String xsex = jtsex.getText();
                    String xage = jtage.getText();
                    String xrelation = jtrelation.getText();
                    String xlocation = jtlocation.getText();
                    String xphone = jtphone.getText();
                    String xwork = jtwork.getText();


                    String sql = "INSERT INTO family(pno,name,sex,age,relation,location,phone,zhiwu) "
                            + "values(?,?,?,?,?,?,?,?)";

                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        System.out.println("JBDC 加载成功!");
                    } catch (Exception a) {
                        System.out.println("JBDC 狗带!");
                        a.printStackTrace();
                    }
                    try {
                        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/work", "root", "123456");

                        ps = conn.prepareStatement(sql);

                        ps.setString(1, xpno);
                        ps.setString(2, xname);
                        ps.setString(3, xsex);
                        ps.setString(4, xage);
                        ps.setString(5, xrelation);
                        ps.setString(6, xlocation);
                        ps.setString(7, xphone);
                        ps.setString(8, xwork);
                        ps.executeUpdate();
                        if (e.getSource() == buttonadd) {
                            javax.swing.JOptionPane.showMessageDialog(null, "添加成功!");
                        }

                    } catch (SQLException b) {
                        System.out.println("添加失败！请重新添加");
                        if (e.getSource() == buttonadd) {
                            javax.swing.JOptionPane.showMessageDialog(null, "添加失败！！！重新添加！添加操作结束!");

                        }
                    } finally {

                        try {
                            conn.close();
                            System.out.println("MySQL 关闭成功");
                        } catch (SQLException c) {
                            System.out.println("MySQL 关闭失败 ");
                            c.printStackTrace();
                        }
                    }
                }
            }
        });
        buttonreturn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {dispose();
            }
        });
        this.setTitle("添加教师信息");
        this.setLayout(new GridLayout(9, 1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpage);
        this.add(jprelation);
        this.add(jplocation);
        this.add(jpphone);
        this.add(jpwork);
        this.add(jpforbutton);
        this.setLocation(400, 300);
        this.setSize(350, 300);
        this.setVisible(true);
    }

}

